package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.Employee;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //返回addEmployee页面
    @RequestMapping("/add")
    public String addEmployee(){
        return "addEmployee";
    }

    //返回updateEmployee页面
    @RequestMapping("/update")
    public String updateEmployee(HttpServletRequest request){
        String e_id = request.getParameter("e_id");
        //根据id查询员工信息
        Employee employee = employeeService.queryOneEmployee(Integer.parseInt(e_id));
        request.setAttribute("Employee",employee);
        return "updateEmployee";
    }

    //查询所有员工
    @RequestMapping("/queryAllEmployee")
    public String queryAllEmployee(ModelMap modelMap, Integer pageNumber, Integer pageSize,String e_name){
        System.out.println(e_name);
        if(pageNumber == null){
            pageNumber = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageData pageData = employeeService.queryAllEmployee(pageNumber,pageSize,e_name);
        modelMap.put("pageData", pageData);
        return "employeeList";
    }

    //新增一个员工
    @RequestMapping("/addEmployee")
    @ResponseBody
    public String addOneEmployee(Employee employee, HttpServletRequest request){
        employee.setE_name(request.getParameter("e_name"));
        employee.setE_sex(request.getParameter("e_sex"));
        employee.setE_username(request.getParameter("e_username"));
        employee.setE_password(request.getParameter("e_password"));
        employee.setE_state(1);
        int i = employeeService.addEmployee(employee);
        if(i>0){
            return "success";
        }
        return "fail";
    }

    //删除一个员工
    @RequestMapping("/deleteEmployee")
    @ResponseBody
    public String deleteEmployee(HttpServletRequest request){
        String e_id = request.getParameter("e_id");
        int i = employeeService.deleteEmployee(Integer.parseInt(e_id));
        if(i>0){
            return "success";
        }
        return "fail";
    }

    //修改员工信息
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public String updateOneEmployee(Employee employee, HttpServletRequest request){
        employee.setE_name(request.getParameter("e_name"));
        employee.setE_sex(request.getParameter("e_sex"));
        employee.setE_id(Integer.parseInt(request.getParameter("e_id")));
        employee.setE_password(request.getParameter("e_password"));
        int i = employeeService.updateEmployee(employee);
        if(i>0){
            return "success";
        }
        return "fail";
    }

}
